//
//  RainbowLampUIView.m
//  UI05_homework_RainbowLamp
//
//  Created by aaaaaa on 16/6/12.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "RainbowLampUIView.h"

@implementation RainbowLampUIView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)dealloc
{
    [_lableArray release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame withCount:(NSInteger)count {
    self = [super initWithFrame:frame];
    
    if (self) {
        self.lableArray = [NSMutableArray arrayWithCapacity:0];
        [self setPropertyWithCount:count];
    }
    
    return self;
}

- (void)setPropertyWithCount:(NSInteger)count{
    

    CGFloat height = self.frame.size.height;
    CGFloat width = self.frame.size.width;
    CGFloat distanceX = width / count / 2;
    CGFloat distanceY = height / count / 2;
    
    for (int i = 0; i < count; i++) {
        UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(distanceX * i, distanceY * i, width - distanceX * i * 2, height - distanceY * i * 2)];
        lable.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
        [self addSubview:lable];
        [lable release];
        [self.lableArray addObject:lable];
    }
    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(changeColor) userInfo:nil repeats:YES];
    
}

- (void)changeColor {
    for (int i = 0; i < self.lableArray.count ; i++) {
        UIColor *temp = self.lableArray[0].backgroundColor;
        if (i < self.lableArray.count - 1) {
            self.lableArray[i].backgroundColor = self.lableArray[i + 1].backgroundColor;
        } else {
            self.lableArray[i].backgroundColor = temp;
        }
    }
    
}












@end
